// pages/order/index.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [],
    height: 0,
    shopimgUrl: app.globalData.shopimgUrl
  },
  onAdd: function () {
    wx.navigateTo({
      url: "/pages/marketadd/index"
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight-100
        })
      }
    });
    let storeId = wx.getStorageSync('storeId');
    _request.get('/Consumables/getStoreConsumables', {
      storeId: storeId,
    }).then((res) => {
      var lists = res.data.data;
      var type = options.type;
      if (type == 'add') {
        var id = options.id;
        var amount = options.amount;
        var consumablesName = options.consumablesName;
        if (id && id != 'undefined') {
          for (let i = 0; i < lists.length; i++) {
            if (lists[i]['id'] == id) {
              lists[i]['consumablesName'] = consumablesName;
              lists[i]['amount'] = amount;
            }
          }
        } else {
          lists.push({
            amount: amount,
            consumablesName: consumablesName
          });
        }
      }
      this.setData({
        lists: lists
      });
    }).catch((error) => {
      console.log(error, 'error');
    })
    wx.enableAlertBeforeUnload({
      message: '内容未保存, 点击返回将导致未保存内容清空, 请确认是否返回?'
    })
  },
  onShow: function () {
    let data = wx.getStorageSync('marketadd')
    if (!data) {
      return
    }
    let list = this.data.lists || []
    data = JSON.parse(data);
    if (data.id) {
      for (var index in list) {
        if (index == data.id) {
          list[index].consumablesName = data.consumablesName;
          list[index].amount = data.amount;
        } 
      }
    } else {
      list.push(data)
    }

    this.setData({
      lists: list
    })
    wx.removeStorageSync('marketadd')
  },
  del: function (e) {
    let index = e.currentTarget.dataset.item;
    var listsTemp = [];
    listsTemp = this.data.lists;
    listsTemp.splice(index, 1);
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '真的要删除吗？',
      success(res) {
        if (res.confirm) {
          _this.setData({
            lists: listsTemp
          });
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  update: function (e) {
    let index = e.currentTarget.dataset.item;
    var market = this.data.lists[index];
    wx.navigateTo({
      url: "/pages/marketadd/index?id=" + index + "&consumablesName=" + market['consumablesName'] + "&amount=" + market['amount']
    })
  },
  onSave: function () {
    let data = [];
    for (let i = 0; i < this.data.lists.length; i++) {
      let item = {
        consumablesName: this.data.lists[i]['consumablesName'],
        amount: this.data.lists[i]['amount']
      }
      data.push(item);
    }
    let storeId = wx.getStorageSync('storeId');
    _request.postObj('/Consumables/saveAll', {
      storeId: storeId,
      data: data,
    }).then((res) => {
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000,
        success: function () {
          wx.disableAlertBeforeUnload();
          let pages = getCurrentPages();
          let prevPage = pages[pages.length - 2];
          prevPage.onLoad();
          wx.navigateBack({
            delta: 1,
          })
        }
      });
    }).catch((error) => {
      console.log(error, 'error');
    })
  }
})